package com.wow.wa.ui;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Typeface;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wow.wa.ui.bean.UICategory;
import com.wow.wa.uicommon.R;

public abstract class BaseDialog {

	private AlertDialog alertDialog = null;
	private Context ct = null;
	private String content = "";
	private View view;// 取得dialog內容

	enum Type {
		warm, error, good
	}

	public BaseDialog(String titleName, Context context, final CategoryBtn btn,
			int inputType) {
		this.ct = context;
		this.content = content;
		final Builder builder = new Builder(context);
		// builder.setMessage(content);
		builder.setTitle(titleName);

		DialogInterface.OnClickListener click = null;

		view = LayoutInflater.from(context).inflate(R.layout.edit_dialog, null);

		TextView title = (TextView) view.findViewById(R.id.title);
		final EditText editText = (EditText) view.findViewById(R.id.editText);
		editText.setInputType(inputType);
		click = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// btn.setText(editText.getText());

				btn.setCategory(editText.getText().toString(), editText
						.getText().toString());
			}
		};

		builder.setNegativeButton(this.ct.getString(android.R.string.ok), click);
		builder.setPositiveButton(this.ct.getString(R.string.close), null);
		title.setText(titleName);

		builder.setView(view);
		// builder.setCustomTitle(customTitleView)
		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);
	}

	public BaseDialog(String titleName, Context context, final Button btn) {
		this.ct = context;
		this.content = content;
		final Builder builder = new Builder(context);
		// builder.setMessage(content);
		builder.setTitle(titleName);

		DialogInterface.OnClickListener click = null;

		view = LayoutInflater.from(context).inflate(R.layout.edit_dialog, null);

		TextView title = (TextView) view.findViewById(R.id.title);
		final EditText editText = (EditText) view.findViewById(R.id.editText);

		click = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				btn.setText(editText.getText());

			}
		};

		builder.setNegativeButton(this.ct.getString(android.R.string.ok), click);

		title.setText(titleName);

		builder.setView(view);
		// builder.setCustomTitle(customTitleView)
		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);
	}

	public BaseDialog(String titleName, Context context, String[] title) {
		this.ct = context;
		this.content = content;

		Activity ac = (Activity) context;

		view = ac.getLayoutInflater().inflate(R.layout.item_list_view, null);

		ListAdapter adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_list_item_1, title);

		ListView listView = (ListView) view.findViewById(R.id.listView);
		TextView titleLabel = (TextView) view.findViewById(R.id.title);

		listView.setAdapter(adapter);

		titleLabel.setText(titleName);

		final Builder builder = new Builder(context);
		// builder.setMessage(content);
		builder.setTitle(titleName);
		builder.setNegativeButton(this.ct.getString(android.R.string.cancel),
				null);
		builder.setView(view);
		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);

	}

	public BaseDialog(String titleName, Context context,
			List<UICategory> categorys) {
		this.ct = context;
		this.content = content;

		Activity ac = (Activity) context;

		view = ac.getLayoutInflater().inflate(R.layout.item_list_view, null);

		BaseAdapter adapter = new CategoryAdatper(categorys, context);

		ListView listView = (ListView) view.findViewById(R.id.listView);
		TextView titleLabel = (TextView) view.findViewById(R.id.title);

		listView.setAdapter(adapter);

		titleLabel.setText(titleName);

		final Builder builder = new Builder(context);
		// builder.setMessage(content);
		builder.setTitle(titleName);
		builder.setNegativeButton(this.ct.getString(android.R.string.cancel),
				null);
		builder.setView(view);
		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);

	}

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public BaseDialog(String content, Context context) {
		this.ct = context;
		this.content = content;

		final Builder builder = new Builder(context);
		builder.setMessage(content);
		builder.setNegativeButton(this.ct.getString(android.R.string.ok), null);

		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);

	}

	public BaseDialog(String content, Context context,
			DialogInterface.OnClickListener actionListener) {
		this.ct = context;
		this.content = content;

		final Builder builder = new Builder(context);
		builder.setMessage(content);
		builder.setNegativeButton(this.ct.getString(android.R.string.ok),
				actionListener);
		builder.setPositiveButton(this.ct.getString(android.R.string.cancel),
				null);

		alertDialog = builder.setTitle(getTitle()).create();
		alertDialog.setCancelable(false);

	}

	public abstract String getTitle();

	public Context getCt() {
		return ct;
	}

	public void setCt(Context ct) {
		this.ct = ct;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void show() {
		if (!this.alertDialog.isShowing()) {
			alertDialog.show();
		}

	}

	public void hide() {

		alertDialog.dismiss();
	}

}
